#include <jni.h>
#include <string>
#include  "android/log.h"
#include "JavaLister.h"
#include "ZYThread.h"

#define LOG_TAG  "LUCAS"
#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)


JavaVM* gJavaVM = nullptr;

JavaLister *gJavaLister = nullptr;
ZYThreadPool mThreadPool(2);





static void test()
{
    while(1) {
        static int index= 0;
        std::string message = std::string("this is a callback from C++ codes in JavaLister_")+ std::to_string(index++);
        gJavaLister->sendMessage(message);
    }
}

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
    gJavaVM = vm;
    return JNI_VERSION_1_6;
}


extern "C" JNIEXPORT jstring
JNICALL
Java_com_example_c_1call_1java_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_c_1call_1java_MainActivity_callJava(JNIEnv *env, jclass clazz) {
    // TODO: implement callJava()

//    auto clazz1 = env->FindClass("com/example/c_call_java/MainActivity");
//    jmethodID callbackID = env->GetStaticMethodID(clazz1, "callback", "(Ljava/lang/String;)V");
//    if(callbackID==0){
//        LOGD("find callback error");
//        return;
//    }
//    LOGD("find callback ");
//
//    jstring result = env->NewStringUTF("this is a callback from C++ codes");
//    //invoke callback method
//    env->CallStaticVoidMethod(clazz1,callbackID, result);

    gJavaLister = new JavaLister(gJavaVM,env);

    mThreadPool.enqueue(std::bind(&test));

}